<?php

namespace App\Models\Member;

use App\Models\BaseModel;
use Config, DB, Auth;
use App\Common\ValidateHandler;

class Withdraw extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'user_id', 'bank_name', 'bank_code', 'deposit', 
        'accepter_name', 'accepter_phone', 'withdraw', 'service_fee', 'status' 
        ,'apply_at','examine_at', 'refuse_at', 'why', 'created_at', 'updated_at'
    ];

    // 通过用户iD 获取提现记录列表
    public function lists($id)
    {
        // 提现金额 / 提现时间 / 提现状态 / 拒绝原因(小字 , 只有已拒绝的才显示)
        $lists = $this->where('user_id', $id)->select('id', 'withdraw', 'status', 'why', 'apply_at', 'bank_code')->orderBy('id', 'desc')->paginate(10);
        if ($lists || $lists == []) {
            $status = Config::get("const.withdraw_status_str");
            return $this->successClient(['lists' => $lists, 'status' => $status], '获取提现记录列表');
        } else {
            return $this->errorClient($lists, '获取提现记录列表失败!'); 
        }
    }

    // 插入提现记录数据 -> 待审核的提现数据
    public function insertWithdraw($withdrawRecord)
    {
        $arrInsert = [];
        $arrInsert['store_id'] = $withdrawRecord['store_id'];
        $arrInsert['user_id'] = $withdrawRecord['user_id'];
        $arrInsert['bank_name'] = $withdrawRecord['bank_name'];
        $arrInsert['bank_code'] = $withdrawRecord['bank_code'];
        $arrInsert['deposit'] = $withdrawRecord['deposit'];
        $arrInsert['accepter_name'] = $withdrawRecord['accepter_name'];
        $arrInsert['accepter_phone'] = $withdrawRecord['accepter_phone'];
        $arrInsert['withdraw'] = $withdrawRecord['withdraw'];
        $arrInsert['service_fee'] = $withdrawRecord['service_fee'];
        $arrInsert['status'] = 0;
        $arrInsert['apply_at'] = date('Y-m-d H:i:s');
        $insert = $this->insertGetId($arrInsert);
        if ($insert) {
            return $this->successClient($insert, '插入提现记录数据成功');
        } else {
            return $this->errorClient($insert, '插入提现记录数据失败!'); 
        }
    }
}
